package com.atguigu.fruit.controller;

import com.atguigu.fruit.bean.Fruit;
import com.atguigu.fruit.dao.FruitDao;
import com.atguigu.fruit.dao.impl.FruitDaoImpl;

import java.util.List;
import java.util.Scanner;

/**
 * @author BruceLee
 * @create 2022-02-28 20:15
 */
public class FruitController {
    FruitDao fruitDao = new FruitDaoImpl();
    Scanner scan = new Scanner(System.in);

    //显示主菜单
    public int showMainMenu(){
        System.out.println("=================欢迎使用水果库存系统=====================");
        System.out.println("1.查看水果库存列表");
        System.out.println("2.添加水果库存信息");
        System.out.println("3.查看特定水果库存信息");
        System.out.println("4.水果下架");
        System.out.println("5.退出");
        System.out.println("======================================================");
        System.out.print("请选择：");

        return scan.nextInt();
    }


    //查看库存列表
    public void showFruitList(){
        System.out.println("------------------------------------------------------");
        System.out.println("FID"+ "\t\t"+"名称"+ "\t\t"+"单价"+ "\t\t"+"库存"+ "\t\t"+"备注信息"+ "\t\t");
        List<Fruit> fruitList = fruitDao.getFruitList();
        if(fruitList == null || fruitList.size()<=0){
            System.out.println("对不起，库存为空！");
        }else {
            for (int i = 0;i < fruitList.size(); i++){
                Fruit fruit = fruitList.get(i);
                System.out.println(fruit);
            }
        }
        System.out.println("------------------------------------------------------");

    }


    //查看指定水果库存信息
    public void showFruitInfo(){
        System.out.print("请输入指定水果名称：");
//        String name = scan.nextLine();  用nextLine会报错
        String name = scan.next();

        Fruit fruit = fruitDao.getFruitByFname(name);
        if(fruit == null){
            System.out.println("对不起，没有找到指定的水果库存记录！");
        }else {
            System.out.println("------------------------------------------------------");
            System.out.println("FID"+ "\t\t"+"名称"+ "\t\t"+"单价"+ "\t\t"+"库存"+ "\t\t"+"备注信息"+ "\t\t");
            System.out.println(fruit);
            System.out.println("------------------------------------------------------");
        }

    }


    //添加水果
    public void addFruit(){
        System.out.print("请输入水果名称：");
        String name = scan.next();
        Fruit fruit = fruitDao.getFruitByFname(name);
        /*
           问题所在 --> 将更新和添加颠倒位置程序出错
           问题已解决：使用nextLine的原因
         */
        if(fruit == null){
            System.out.print("请输入水果单价：");
            int price = scan.nextInt();
            System.out.print("请输入水果库存量：");
            int fcount = scan.nextInt();
            System.out.print("请输入水果描述信息：");
            String remark = scan.next();

            fruit = new Fruit(0,name,price,fcount,remark);
            fruitDao.addFruit(fruit);
        }else {
            System.out.print("请输入追加的库存量：");
            int count = scan.nextInt();
            fruit.setFcount(fruit.getFcount() + count);
            fruitDao.updateFruit(fruit);
        }

        System.out.println("添加成功！");

    }


    //水果下架
    public void delFruit(){
        System.out.print("请输入水果名称：");
        String name = scan.next();
        Fruit fruit = fruitDao.getFruitByFname(name);
        if(fruit == null){
            System.out.println("水果不存在！");
        }else {
            System.out.print("是否确认下架？(Y/N)");
            String del = scan.next();
            if ("Y".equalsIgnoreCase(del)){
                fruitDao.delFruit(name);
                System.out.println("下架成功！");
            }

        }

    }


    //退出
    public boolean exit(){
        System.out.print("是否退出：（Y/N）");
        String slt = scan.next();
//        return "Y".equalsIgnoreCase(next); --> 此处需要加一个 非
        return !"Y".equalsIgnoreCase(slt);
    }

}
